# -*- coding: utf-8 -*-
import argparse
import string

# 更新系统脚本

# python main.py --project xhj --development testing

# 1 小火箭测试环境
# 2 小火箭正式环境
# 3 大掌柜测试环境
# 4 大掌柜正式环境
from config import server
from scripts.process import run_update_code


def main(args):
    # 初始化配置
    update_key = args.update
    config_str = f"{args.project.upper()}{args.environment.title()}Config"
    server_params_list = dir(server)
    if config_str not in server_params_list or update_key not in [None, "backend", "frontend"]:
        raise ValueError("参数错误，请输入正确的参数!")

    config = getattr(server, config_str)
    server.InitConfig.config = config
    # 更新代码
    print("update_key", update_key)
    config_dict = server.CONFIG_INFO_DICT
    RED = "\033[31m"
    RESET = "\033[0m"
    project = f"{RED}{config_dict[args.project]}{RESET}"
    environment = f"{RED}{config_dict[args.environment]}{RESET}"
    update = f"{RED}{config_dict[args.update]}{RESET}"

    input_str = input(f"即将更新：[{RED}{project}{RESET}]的[{environment}]环境的[{update}]代码 \n请确认(y/Y)：")
    if input_str.lower() in ["", "y"]:
        run_update_code(update_key)
        print(f'done! {args.project} {args.environment} {args.update} update success')
    else:
        print("请修改参数后重新运行！")


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Update_Project")
    parser.add_argument("-p", "--project", help="项目简称[xhj, dzg]", required=True, type=str)
    parser.add_argument("-e", "--environment", help="项目环境[production, testing]", required=True, type=str)
    # 更新前端/后端,未指定，都更新
    parser.add_argument("-u", "--update", help="更新前端/后端[backend, fronted]", type=str)
    args = parser.parse_args()

    main(args)
